package user.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import core.constants.Constant;
import core.domain.PageQuery;
import core.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import user.domain.exam.vo.ExamVO;
import user.domain.message.vo.MessageTextVO;
import user.mapper.MessageTextMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MessageCacheManage {
    @Autowired
    private RedisService redisService;

    @Autowired
    private MessageTextMapper messageTextMapper;

    public Long getListSize(Long userId) {
        String key = getUserMessageListKey(userId);
        return redisService.getListSize(key);
    }

    private String getUserMessageListKey(Long userId){
        return Constant.USER_MESSAGE_LIST + userId;
    }

    private String getMessageDetailKey(Long textId){
        return Constant.MESSAGE_DETAIL + textId;
    }

    public void refreshCache(Long userId, List<MessageTextVO> messageTextVOList) {
        if(CollectionUtil.isEmpty(messageTextVOList)){
            return;
        }
        String key = getUserMessageListKey(userId);
        List<Long> testIdList = messageTextVOList.stream().map(MessageTextVO::getTextId).toList();
        redisService.rightPushAll(key, testIdList);
        Map<String, MessageTextVO> messageTextVOMap = new HashMap<>();
        for(MessageTextVO messageTextVO : messageTextVOList){
            String detailKey = getMessageDetailKey(messageTextVO.getTextId());
            messageTextVOMap.put(detailKey, messageTextVO);
        }
        redisService.multiSet(messageTextVOMap);
    }

    public List<MessageTextVO> getMessageTextVOList(PageQuery query, Long userId) {
        int start = (query.getPageNum() - 1) * query.getPageSize();
        int end = start + query.getPageSize() - 1;
        String userMessageListKey = getUserMessageListKey(userId);
        List<Long> textIdList = redisService.getCacheListByRange(userMessageListKey, start, end, Long.class);
        List<MessageTextVO> messageTextVOList = assembleMessageTextVOList(textIdList);
        if (CollectionUtil.isEmpty(messageTextVOList)) {
            // 说明 redis 中数据存在问题, 从数据库中查询, 并刷新缓存
            messageTextVOList = getExamVOByDB(query, userId);
            refreshCache(userId, messageTextVOList);
        }
        return messageTextVOList;
    }

    private List<MessageTextVO> assembleMessageTextVOList(List<Long> textIdList) {
        if(CollectionUtil.isEmpty(textIdList)){
            return null;
        }
        List<String> detailKeyList = new ArrayList<>();
        for (Long textId : textIdList) {
            detailKeyList.add(getMessageDetailKey(textId)); // 拼接key
        }
        List<MessageTextVO> messageTextVOList = redisService.multiGet(detailKeyList, MessageTextVO.class);
        CollUtil.removeNull(messageTextVOList);
        if(CollectionUtil.isEmpty(messageTextVOList) || messageTextVOList.size() != textIdList.size()){
            return null;
        }
        return messageTextVOList;
    }

    private List<MessageTextVO> getExamVOByDB(PageQuery query, Long userId) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return messageTextMapper.selectUserMessageList(userId);
    }
}
